<!-- -->
<template>
  <div class="public">
    <div class="login-info">
      <h3>账号密码登录</h3>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号:" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <div class="btn">
          <el-form-item>
            <el-button class="loginBtn" type="primary" @click="submitForm('ruleForm')">登录</el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "@/api";
import { auth } from "@/utils";
export default {
  // import引入到组件需要注入到对象中才能使用
  components: {},
  data() {
    // 这里存放数据
    return {
      ruleForm: {},
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      },
      auth //走一遍生命周期
    };
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监听data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          login(this.ruleForm).then(res => {
            if (res.code === "203") {
              this.$message.success(res.msg);
              localStorage.setItem("newtoken", res.data);
              this.$router.push("/");
              // 登陆完成 已经存入token
              auth();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          return false;
        }
      });
    }
  },
  //生命周期 - 创建完成 (可以访问当前this实例)
  created() {},
  //生命周期 - 挂载完成 (可以访问DOM元素)
  mounted() {}
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style lang="scss" scoped>
.public {
  background-image: url("../../assets/1.jpg");
  background-repeat: no-repeat; //不重复
  background-size: 100% 100%; // 满屏
}
.el-carousel__item {
  width: 100%;
  height: 100%;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}

.login-info {
  padding: 20px 40px 0 0px;
  position: absolute;
  right: 20px;
  top: 234px;
  width: 470px;
  height: 250px;
  z-index: 22;
  border: 2px solid steelblue;
  border-radius: 15px;
  background: #fff;
  h3 {
    padding-left: 20px;
    padding-bottom: 20px;
  }
}
.btn {
  width: 100%;
  display: flex;
  justify-content: center;
  .loginBtn {
    width: 200px;
  }
}
</style>
